package volunteer.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;

import common.Constant;
import pay.activity.Pay;
import personalPage.activity.PersonalPage;
import question.activity.QuestionDetail;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import view.MyImageLoader;
import volunteer.bean.VolunteerBean;
import utils.MyLogUtils;

/**
 * Created by wangic on 2017/2/26.
 */
public class VolunteerStickyListHeaderListViewAdapter extends BaseAdapter implements StickyListHeadersAdapter {

    private LayoutInflater inflater;
    private Context context;

    private ArrayList<VolunteerBean> arrayList;

    public VolunteerStickyListHeaderListViewAdapter(Context context, ArrayList<VolunteerBean> arrayList) {
        this.inflater = LayoutInflater.from(context);
        this.arrayList = arrayList;
        this.context = context;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        myHeaderHolder myHeaderHolder;
        String answered = context.getResources().getString(R.string.volunteer_item7);
        String evaluate = context.getResources().getString(R.string.volunteer_item8);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.volunteer_list_itemheader, null);
            myHeaderHolder = new myHeaderHolder();
            myHeaderHolder.volunteer_list_itemheader_tv = (TextView) convertView.findViewById(R.id.volunteer_list_itemheader_tv);
            convertView.setTag(myHeaderHolder);
        } else {
            myHeaderHolder = (VolunteerStickyListHeaderListViewAdapter.myHeaderHolder) convertView.getTag();
        }
        if (Constant.VOLUNTEER_ANSWER == arrayList.get(position).getPro_id()) {
            myHeaderHolder.volunteer_list_itemheader_tv.setText(answered);
        } else {
            myHeaderHolder.volunteer_list_itemheader_tv.setText(evaluate);
        }
        return convertView;
    }

    class myHeaderHolder {
        TextView volunteer_list_itemheader_tv;//已收到的评价或者已回答的问题
    }


    @Override
    public long getHeaderId(int position) {
        return arrayList.get(position).getPro_id();
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        switch (arrayList.get(position).getPro_id()) {
            case Constant.VOLUNTEER_ANSWER:
                MyQuestionHolder myHolder;
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.volunteer_answer_list_item, null);
                    myHolder = new MyQuestionHolder();
                    myHolder.volunteer_answer_item_icon = (ImageView) convertView.findViewById(R.id.volunteer_answer_item_icon);
                    myHolder.volunteer_answer_item_name = (TextView) convertView.findViewById(R.id.volunteer_answer_item_name);
                    myHolder.volunteer_answer_item_question = (TextView) convertView.findViewById(R.id.volunteer_answer_item_question);
                    myHolder.volunteer_answer_item_isCost = (LinearLayout) convertView.findViewById(R.id.volunteer_answer_item_isCost);
                    myHolder.volunteer_answer_item_date = (TextView) convertView.findViewById(R.id.volunteer_answer_item_date);
                    myHolder.volunteer_answer_item_pay = (TextView) convertView.findViewById(R.id.volunteer_answer_item_pay);
                    myHolder.volunteer_answer_item_cost = (TextView) convertView.findViewById(R.id.volunteer_answer_item_cost);
                    myHolder.volunteer_answer_item_answer = (TextView) convertView.findViewById(R.id.volunteer_answer_item_answer);
                    convertView.setTag(myHolder);
                } else {
                    myHolder = (MyQuestionHolder) convertView.getTag();
                }
                MyImageLoader.showImage(arrayList.get(position).getIcon(),myHolder.volunteer_answer_item_icon);
                myHolder.volunteer_answer_item_name.setText(arrayList.get(position).getName());
                myHolder.volunteer_answer_item_question.setText(arrayList.get(position).getQuestion());
                myHolder.volunteer_answer_item_date.setText(arrayList.get(position).getTime());
                myHolder.volunteer_answer_item_answer.setText(arrayList.get(position).getAnswerContent());
                if("free".equals(arrayList.get(position).getType())){
                    //免费问题
                    myHolder.volunteer_answer_item_isCost.setVisibility(View.GONE);
                    myHolder.volunteer_answer_item_answer.setVisibility(View.VISIBLE);
                }else{
                    if("0".equals(arrayList.get(position).getIsPay())){
                        //未付费
                        myHolder.volunteer_answer_item_isCost.setVisibility(View.VISIBLE);
                        myHolder.volunteer_answer_item_answer.setVisibility(View.GONE);
                        myHolder.volunteer_answer_item_isCost.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent i = new Intent(context, Pay.class);
                                i.putExtra("dialog_id", arrayList.get(position).getId());
                                i.putExtra("position", position);
                                i.putExtra("price", Constant.QUESTIONWATCH_PRICE);
                                i.putExtra("payType", Constant.QUESTIONWATCH);
                                ((Activity)context).startActivityForResult(i,2);
                            }
                        });
                    }
                    else{
                        myHolder.volunteer_answer_item_isCost.setVisibility(View.GONE);
                        myHolder.volunteer_answer_item_answer.setVisibility(View.VISIBLE);
                    }
                }
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(arrayList.get(position).getId()!=null) {
                            Intent intent = new Intent(context, QuestionDetail.class);
                            intent.putExtra("dialogId", arrayList.get(position).getId());
                            context.startActivity(intent);
                        }
                    }
                });

                break;
            case Constant.VOLUNTEER_EVALUATE:
            default:
                MyEvaluateHolder myEvaluateHolder;
                if (convertView == null) {
                    myEvaluateHolder = new MyEvaluateHolder();
                    convertView = inflater.inflate(R.layout.volunteer_evaluate_list_item, null);
                    myEvaluateHolder.volunteer_evaluate_item_name = (TextView) convertView.findViewById(R.id.volunteer_evaluate_item_name);
                    myEvaluateHolder.volunteer_evaluate_item_icon = (RoundedImageView) convertView.findViewById(R.id.volunteer_evaluate_item_icon);
                    myEvaluateHolder.volunteer_evaluate_item_date = (TextView) convertView.findViewById(R.id.volunteer_evaluate_item_date);
                    myEvaluateHolder.volunteer_evaluate_item_answer = (TextView) convertView.findViewById(R.id.volunteer_evaluate_item_answer);
                    convertView.setTag(myEvaluateHolder);
                } else {
                    myEvaluateHolder = (MyEvaluateHolder) convertView.getTag();
                }
                myEvaluateHolder.volunteer_evaluate_item_name.setText(arrayList.get(position).getName());
                myEvaluateHolder.volunteer_evaluate_item_date.setText(arrayList.get(position).getTime());
                myEvaluateHolder.volunteer_evaluate_item_answer.setText(arrayList.get(position).getCommentContent());
                MyImageLoader.showImage(arrayList.get(position).getIcon(),myEvaluateHolder.volunteer_evaluate_item_icon);
                break;
        }
        return convertView;
    }


    class MyQuestionHolder {
        ImageView volunteer_answer_item_icon;
        TextView volunteer_answer_item_name;
        TextView volunteer_answer_item_question;
        LinearLayout volunteer_answer_item_isCost;
        TextView volunteer_answer_item_date;
        TextView volunteer_answer_item_pay;
        TextView volunteer_answer_item_cost;
        TextView volunteer_answer_item_answer;

    }

    class MyEvaluateHolder{
        RoundedImageView volunteer_evaluate_item_icon;
        TextView volunteer_evaluate_item_name;
        TextView volunteer_evaluate_item_date;
        TextView volunteer_evaluate_item_answer;
    }

}
